//
//  FriendTableViewCell.m
//  自定义高度
//
//  Created by 逗比小胖！ on 16/2/29.
//  Copyright © 2016年 徐晨. All rights reserved.
//

#import "FriendTableViewCell.h"
#import "Friend.h"
#import "FriendFrame.h"


@interface FriendTableViewCell()
@property(nonatomic,weak)UIImageView *iconView;
@property(nonatomic,weak)UIImageView *picView;
@property(nonatomic,weak)UILabel *nameLab;
@property(nonatomic,weak)UILabel *textLab;
@property(nonatomic,weak)UIButton *recommendBtn;
@property(nonatomic,weak)UIButton *zanBtn;
@property(nonatomic,weak)UIButton *relayBtn;

@property(nonatomic,assign)int index;



@end

@implementation FriendTableViewCell

+ (instancetype)friendcellWithTableView:(UITableView *)tableView{
    static NSString *ID = @"friendCell";
    static BOOL isRegist = NO;
    if(isRegist == NO){
        isRegist = YES;
        [tableView registerClass:[self class] forCellReuseIdentifier:ID];
    }
    return [tableView dequeueReusableCellWithIdentifier:ID];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        UIImageView *iconView = [[UIImageView alloc]init];
        self.iconView = iconView;
        [self.contentView addSubview:iconView];
        
        UIImageView *picView = [[UIImageView alloc]init];
        self.picView  = picView;
        [self.contentView addSubview:picView];
        
        UILabel *textLab = [[UILabel alloc]init];
        textLab.font = kText;
        textLab.numberOfLines = 0;
        self.textLab = textLab;
        [self.contentView addSubview:textLab];
        
        UILabel *nameLab = [[UILabel alloc]init];
        nameLab.font = kName;
        self.nameLab = nameLab;
        [self.contentView addSubview:nameLab];
        
        UIButton * btn1 = [[UIButton alloc]init];
        [btn1 setTitle:@"评论" forState:UIControlStateNormal];
        
//        [btn1 addTarget:self action:@selector(recommendBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn1.backgroundColor = [UIColor redColor];
        self.recommendBtn = btn1;
        [self.contentView addSubview:btn1];
        
        UIButton * btn2 = [[UIButton alloc]init];
         btn2.backgroundColor = [UIColor greenColor];
        [btn2 setTitle:@"点赞" forState:UIControlStateNormal];
        
        btn2.selected = NO;
        self.index = 0;
        [btn2 setTitle:[NSString stringWithFormat:@"%d",self.index] forState:UIControlStateNormal];
        [btn2 addTarget:self action:@selector(dianzanBtn:) forControlEvents:UIControlEventTouchUpInside];

        self.zanBtn = btn2;
        [self.contentView addSubview:btn2];
        
        UIButton *btn3 = [[UIButton alloc]init];
        
         btn3.backgroundColor = [UIColor blackColor];
        [btn3 setTitle:@"分享" forState:UIControlStateNormal];
        self.relayBtn = btn3;
        [self.contentView addSubview:btn3];
        
        
    }
    return  self;
}

- (void)dianzanBtn:(UIButton *)btn{
    if (btn.selected == NO) {
        self.index += 1;
        [btn setTitle:[NSString stringWithFormat:@"%d",self.index] forState:UIControlStateNormal];
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您已经关注了" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        
        btn.selected = YES;
    }else {
        btn.selected = NO;
        self.index -= 1;
        [btn setTitle:[NSString stringWithFormat:@"%d",self.index] forState:UIControlStateNormal];
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您已经取消关注了" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];

    }

}


- (void)setFriendFrame:(FriendFrame *)friendFrame{
    _friendFrame =friendFrame;
    [self setData];
    [self setDataFrame];
    
    
}

- (void)setData{
    Friend *friend = self.friendFrame.friend;
    self.iconView.image = [UIImage imageNamed:friend.icon];
    self.textLab.text = friend.text;
    
    self.nameLab.text = friend.name;
    
    
    if(friend.picture){
        self.picView.hidden = NO;
        self.picView.image = [UIImage imageNamed:friend.picture];
    }else{
        self.picView.hidden = YES;
    }
}

- (void)setDataFrame{
    self.iconView.frame = self.friendFrame.iconImageViewFrame;
    self.picView.frame = self.friendFrame.picImageViewFrame;
    self.nameLab.frame = self.friendFrame.nameLabFrame;
    self.textLab.frame = self.friendFrame.textLabFrame;
    
    self.recommendBtn.frame = self.friendFrame.btn1Frame;
    self.zanBtn.frame = self.friendFrame.btn2Frame;
    self.relayBtn.frame = self.friendFrame.btn3Frame;

    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
